package com.example.marcus.tokentest;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.View;

/**
 * Created by Marcus on 2015/5/10.
 */
public class ChangeColorwithText extends View {

    private int color = 0xFF45C01A;
    private Bitmap bitmap;
    private String mtext = "default";
    private int mtextSzie = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics());

    private Bitmap finalbitmap;
    private Canvas mCanvas;
    private Paint mPaints;

    private int mAlpha;
    private Rect iconRect;
    private Rect textRect;
    private Paint textPaint;

    public ChangeColorwithText(Context context) {
        super(context);
        initView(context,null,0);
    }

    public ChangeColorwithText(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context,attrs,0);
    }

    public ChangeColorwithText(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs,defStyleAttr);
    }

    public void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.ChangeColorwithText);
        int n = ta.getIndexCount();
        for (int i = 0; i < n; i++) {
            int attr = ta.getIndex(i);
            switch (attr) {
                case R.styleable.ChangeColorwithText_nd_icon:
                    BitmapDrawable bd = (BitmapDrawable) ta.getDrawable(attr);
                    bitmap = bd.getBitmap();
                    break;
                case R.styleable.ChangeColorwithText_nd_text:
                    String tmp = ta.getString(attr);
                    mtext = tmp;
                    break;
                case R.styleable.ChangeColorwithText_nd_text_size:
                    mtextSzie = (int) ta.getDimension(attr, TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 12, getResources().getDisplayMetrics())
                    );
                    break;
                case R.styleable.ChangeColorwithText_nd_color:
                    color = ta.getColor(attr, 0xFF45C01A);
                    break;
            }
        }
        ta.recycle();
        // other
        textRect = new Rect();
        textPaint = new Paint();
        textPaint.setTextSize(mtextSzie);
        textPaint.setColor(0xFF555555);
        textPaint.getTextBounds(mtext, 0, mtext.length(), textRect);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        int iconWidth = Math.min(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
                getMeasuredHeight() - getPaddingTop() - getPaddingBottom() - textRect.height());
        int left = getMeasuredWidth() / 2 - iconWidth / 2;
        int top = (getMeasuredHeight() - textRect.height()) / 2 - iconWidth / 2;
        iconRect = new Rect(left, top, left + iconWidth, top + iconWidth);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawBitmap(bitmap, null, iconRect, null);

        super.onDraw(canvas);
    }
    //
//    public ChangeColorwithText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
//        super(context, attrs, defStyleAttr, defStyleRes);
//    }
}
